<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Log;

class PyAdmissionInformation extends BaseModel
{
    //绑定地区
    public function bindLocation(){
        return $this->hasOne("PyAttr","attr_id","ain_location_id");
    }

    //绑定年份
    public function bindYear(){
        return $this->hasOne("PyAttr","attr_id","ain_year_id");
    }

    /*
     *导入excel数据
     * $attr_id 所导入数据的分类
     * $file 上传的Excel文件
     * */
    public function inputExcel($attr_id,$fileData){
        $attrInfo=model("PyAttr")->where("attr_id","=",$attr_id)->find();
        $location=$attrInfo['attr_name'];
        $year=$attrInfo['attr_value'];
        $count=count($fileData);
        $num=config("setting.excelImportNum");

        //将fileData按照分几次插入数据
        //开启实务操作
        Db::startTrans();
        try{
            for($i=0;$i<$count;$i=$i+$num){
                $sql=$this->assembleExcelSql($fileData,$year,$location,$i,($i+$num)>=$count?$count:($i+$num));
                $this->query($sql);
            }
        }catch (Exception $exception){
            Db::rollback();;
            Log::write("PyAdmissionInformation.inputExcel : ".$exception->getMessage());
            return "数据插入异常";
        }
        Db::commit();
        return 1;
    }

    //清除数据
    public function deleteInformation($inputData){
        $condition=[];
        //对应关系详见setting.php文件
        $condition['ain_location_id']=['eq',$inputData['location']];
        $condition['ain_year_id']=['eq',$inputData['year']];
        try{
            $this->where($condition)->delete();
        }catch (Exception $exception){
            Log::write("Information.deleteInformation: ".$exception->getMessage());
            return "系统异常请联系技术人员";
        }
        return 1;
    }


    //组装excel插入的数据
    //拼装sql语句
    public function assembleExcelSql($fileData,$year,$location, $start,$end)
    {
        $sql="insert into py_admission_information (ain_year_id,ain_location_id,ain_school_code,ain_school_name,
            ain_profession_code,ain_profession_name,ain_plan_num,ain_grade,ain_position) values ";

        for($i=$start;$i<$end;$i++){
            $temp="(%s,%s,'%s','%s','%s','%s','%s','%s','%s'),";
            $tempStr=sprintf($temp,$year,$location,$fileData[$i][0],$fileData[$i][1],$fileData[$i][2],$fileData[$i][3],$this->changeType($fileData[$i][4])
            ,$this->changeType($fileData[$i][5]),$this->changeType($fileData[$i][6]));
            $sql.=$tempStr;
        }

        $sql=trim($sql,",");
        $sql.=";";
        return $sql;
    }

    //转换 float => string
    public function changeType($value){
        if($value==null){
            //如果为空，写入空字符串
            return "";
        }
        $value=(string)(int)$value;
        return $value;
    }


    //校验报告是否存在
    public function checkIsset($input){
//        year=2&location=1&slo_school_code=&slo_school_name=&slo_profession_code=&slo_profession_name=&slo_grade_min=&slo_grade_max=&slo_position_min=&slo_position_max=
        //条件处理
        $condition=$this->getCondition($input);
        $isset=false;
        try{
            $isset=$this->where($condition)->find();
        }catch (Exception $exception){
            Log::write("PyAdmissionInformation checkIsset".$exception->getMessage());
            return "查询异常";
        }
        if(!$isset){
            return "数据不存在";
        }else{
            return 1;
        }
    }

    //处理 inputData=>condition的转变
    public function getCondition($inputData){
        $condition=[];
        $condition['ain_year_id']=['eq',$inputData['year']];
        $condition['ain_location_id']=['eq',$inputData['location']];
        if($inputData['slo_school_code']!=""){
            $condition['ain_school_code']=['eq',$inputData['slo_school_code']];
        }
        if($inputData['slo_school_name']!=""){
            $condition['ain_school_name']=['like',"%".$inputData['slo_school_name']."%"];
        }

        if($inputData['slo_profession_code']!=""){
            $condition['ain_profession_code']=['eq',$inputData['slo_profession_code']];
        }
        if($inputData['slo_profession_name']!=""){
            $condition["ain_profession_name"]=["like","%".$inputData['slo_profession_name']."%"];
        }

        if($inputData['slo_grade_min']!=""&&$inputData['slo_grade_max']!=""){
            $condition['ain_grade']=['between',[(int)$inputData['slo_grade_min'],(int)$inputData['slo_grade_max']]];
        }else if($inputData['slo_grade_min']!=""){
            $condition['ain_grade']=[">",$inputData['slo_grade_min']];
        }else if($inputData['slo_grade_max']!=""){
            $condition['ain_grade']=['<',$inputData['slo_grade_max']];
        }
        if($inputData['slo_position_min']!=""&&$inputData['slo_position_max']!=""){
            $condition['ain_position']=['between',[(int)$inputData['slo_position_min'],(int)$inputData['slo_position_max']]];
        }else if($inputData['slo_position_min']!=""){
            $condition['ain_position']=[">",$inputData['slo_position_min']];
        }else if($inputData['slo_position_max']!=""){
            $condition['ain_position']=['<',$inputData['slo_position_max']];
        }
        return $condition;
    }
}
